﻿using Assignment5;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{

    public partial class Form3 : Form
    {
        Order preOrder;
        Customer currentCustom, newCustom;
        List<Customer> customList;
        List<Goods> goods, availableGoods;
        public Form3(Order o, List<Goods> g, List<Customer> c)
        {
            InitializeComponent();
            preOrder= o;

            currentCustom = o.OrderCustomer;
            customList = c;
            customerBindingSource.DataSource = customList;

            goods = o.Description.GoodsList;
            nowGoodTable.DataSource = goods;

            availableGoods = g;
            goodsBindingSource.DataSource = availableGoods;

            newCustom=o.OrderCustomer;
            preCusId.Text = o.CId.ToString();
            preCusName.Text = o.OrderCustomer.CustomerName.ToString();
        }

        //将客户列表和所有商品列表设置为数据绑定源
        public void Form3_Load(object sender, EventArgs e)
        {
            customerBindingSource.DataSource = customList;
            goodsBindingSource.DataSource = availableGoods;
            nowGoodTable.DataSource = goods;
            
        }

        //获取选中的客户信息
        private void cusTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow doubleClickedRow = cusTable.Rows[e.RowIndex];
                int cid = Convert.ToInt32(doubleClickedRow.Cells["CusIdCol"].Value);
                newCustom = customList.FirstOrDefault(c => c.CustomerId == cid);
                newCusName.Text = newCustom.CustomerName;
                newCusId.Text = newCustom.CustomerId.ToString();
            }
        }

        //修改对应客户信息
        private void changeCusButton_Click(object sender, EventArgs e)
        {
            currentCustom = newCustom;
            preCusId.Text = currentCustom.CustomerId.ToString();
            preCusName.Text = currentCustom.CustomerName.ToString();
        }

        //删除已有商品信息
        private void nowGoodTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow doubleClickedRow = nowGoodTable.Rows[e.RowIndex];
            int gid = Convert.ToInt32(doubleClickedRow.Cells["nowGoodIdCol"].Value);
            Goods toDelGood = goods.FirstOrDefault(g => g.GoodId == gid);
            goods.Remove(toDelGood);
            nowGoodTable.DataSource = null; // 清空数据源
            nowGoodTable.DataSource= goods;
            nowGoodTable.Refresh();
        }

        //添加新商品信息
        private void allGoodTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow doubleClickedRow = allGoodTable.Rows[e.RowIndex];
            int gid = Convert.ToInt32(doubleClickedRow.Cells["allGoodIdCol"].Value);
            Goods toAddGood = availableGoods.FirstOrDefault(g => g.GoodId == gid);
            goods.Add(toAddGood); 
            nowGoodTable.DataSource = null; // 清空数据源
            nowGoodTable.DataSource = goods;
            nowGoodTable.Refresh();
        }

        //确认修改
        private void confirmButton_Click(object sender, EventArgs e)
        {
            Order newOrder = new Order(newCustom);
            goods.ForEach(newOrder.addGoods);
            OrderService orderService=OrderService.getInstance();
            orderService.modifyOrder(newOrder,preOrder);
            MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
